#include <stdio.h>
#include <stdlib.h>

void strncpy(char * s1, const char * s2, int n);

int main(int argc, char * argv[])
{
	
	char input[80];
	char temp[80];
	char * ptr;
	int count;

	puts("请输入要复制的字符串:");
	while (gets(input) && input[0] != '\0')
	{
		count = -1;
		while (count > 0)
		{
			puts("请输入要复制字符的个数：");
			count = atoi(gets(temp));
		}
		strncpy(ptr, input, count);

		printf("复制后的字符串:%s", ptr);
	}

	return 0;
}

void strncpy(char * s1, const char * s2, int n)
{
	// 复制s2 到 s1 
	int i;
	int len;

	len = strlen(s2) - 1;
	n = len > n ? n : len;

	for (i = 0; i < n; i++)
	{	
		s1[i] = s2[i];
	}
	s1[i] = '\0';		// 字符串末尾以'\0'结束
}
